package com.lucky.ebidding.service;

import com.lucky.ebidding.dao.ExpertDao;
import com.lucky.ebidding.dao.ExtSqlDao;
import com.lucky.ebidding.dao.ScoreInfoDao;
import com.lucky.ebidding.dao.ScoreTotalDao;
import com.lucky.ebidding.model.*;
import com.lucky.ebidding.service.interfaces.ScoreServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.OptionalDouble;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * ClassName:ScoreService
 * Package:com.lucky.ebidding.service
 * Description:
 *
 * @Date:2020/5/20 10:29
 * @Author:857251389@qq.com
 */
@Service
public class ScoreService implements ScoreServiceI {
    @Autowired
    private ScoreInfoDao scoreInfoDao;

    @Autowired
    private ScoreTotalDao scoreTotalDao;

    @Autowired
    private ExtSqlDao extSqlDao;

    @Override
    public int insert(Integer zbId, Integer tbId, Integer score, User user) {
        ScoreInfo scoreInfo = new ScoreInfo();
        scoreInfo.setScore(score);
        scoreInfo.setTbId(tbId);
        scoreInfo.setZbId(zbId);
        scoreInfo.setSerialNumber(UUID.randomUUID().toString().substring(0,8)+ LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        scoreInfo.setExpertId(user.getId());
        int i = scoreInfoDao.insert(scoreInfo);
        if(i!=0){
            Expert expert = new Expert();
            expert.setUserId(user.getId());
            expert.setLevel(1);
            extSqlDao.levelAdd(expert);
        }
        return i;
    }

    @Override
    public Integer selectPfByTbIdAndExpertId(Integer id, Integer expertId) {
        ScoreInfoExample scoreInfoExample = new ScoreInfoExample();
        scoreInfoExample.createCriteria().andExpertIdEqualTo(expertId).andTbIdEqualTo(id);
        try {
            List<ScoreInfo> scoreInfos = scoreInfoDao.selectByExample(scoreInfoExample);
            if(scoreInfos.size()>0){
                return scoreInfos.get(0).getScore();
            }
        }catch (Exception e){

        }
        return null;
    }

    @Override
    public ScoreTotal scoreTotal(List<ScoreInfo> scoreInfoList) {
        double score = scoreInfoList.stream().mapToDouble(scoreInfo -> scoreInfo.getScore()).average().getAsDouble();
        int scoreAvg = new Double(score).intValue();

        ScoreTotal scoreTotal = new ScoreTotal();
        scoreTotal.setTbId(scoreInfoList.get(0).getTbId());
        scoreTotal.setZbId(scoreInfoList.get(0).getZbId());
        scoreTotal.setTotalScore(scoreAvg);

        ScoreTotalExample scoreTotalExample = new ScoreTotalExample();
        scoreTotalExample.createCriteria().andZbIdEqualTo(scoreInfoList.get(0).getZbId()).andTbIdEqualTo(scoreInfoList.get(0).getTbId());
        List<ScoreTotal> scoreTotals = scoreTotalDao.selectByExample(scoreTotalExample);
        if(scoreTotals.isEmpty()){
            int i = scoreTotalDao.insert(scoreTotal);
            return scoreTotal;
        }
        return scoreTotals.get(0);
    }

    @Override
    public List<ScoreInfo> selectInfoByTbId(Integer tbId) {
        ScoreInfoExample scoreInfoExample = new ScoreInfoExample();
        scoreInfoExample.createCriteria().andTbIdEqualTo(tbId);
        List<ScoreInfo> scoreInfos = scoreInfoDao.selectByExample(scoreInfoExample);

        return scoreInfos;
    }

}
